package com.example.intermediatecontrols;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.Calendar;

public class DialogActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {

    private TextView tv_unloadRes;
    private TextView tv_showSelectDate;
    private TextView tv_showSelectTime;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_dialog);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        tv_unloadRes = findViewById(R.id.tv_unloadRes);
        Button btn_unload = findViewById(R.id.btn_unload);
        btn_unload.setOnClickListener(this);
        //
        Button btn_selectDate = findViewById(R.id.btn_selectDate);
        DatePicker dp = findViewById(R.id.dp);
        tv_showSelectDate = findViewById(R.id.tv_showSelectDate);
        btn_selectDate.setOnClickListener(v -> {
            String str = String.format("选的的是%s年-%s月-%s日", dp.getYear(), dp.getMonth() + 1, dp.getDayOfMonth());
            tv_showSelectDate.setText(str);
        });
        Button btn_dateDialog = findViewById(R.id.btn_dateDialog);
        btn_dateDialog.setOnClickListener(v -> {
            //获得当前年月日
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog dpd = new DatePickerDialog(this, this, year, month, day);
            dpd.show();
        });
        //
        TimePicker tp = findViewById(R.id.tp);
        tp.setIs24HourView(true);
        Button btn_selectTime = findViewById(R.id.btn_selectTime);
        tv_showSelectTime = findViewById(R.id.tv_showSelectTime);
        btn_selectTime.setOnClickListener(v -> {
            String str = String.format("选的的是%s时-%s分", tp.getHour(), tp.getMinute());
            tv_showSelectTime.setText(str);
        });
        Button btn_timeDialog = findViewById(R.id.btn_timeDialog);
        btn_timeDialog.setOnClickListener(v -> {
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR);
            int minute = calendar.get(Calendar.MINUTE);
            TimePickerDialog tpd = new TimePickerDialog(this, android.R.style.Theme_Holo_Light_Dialog, this, hour, minute, true);
            tpd.show();
        });
    }

    @Override
    public void onClick(View v) {
        //创建提示弹窗构造器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置弹窗标题
        builder.setTitle("尊敬的用户");
        //设置弹窗内容
        builder.setMessage("您确定要卸载吗?");
        //设置提示弹窗按钮点击事件
        builder.setPositiveButton("残忍卸载", (dialog, which) -> {
            tv_unloadRes.setText("已卸载");
        });
        builder.setNegativeButton("再想想", (dialog, which) -> {
            tv_unloadRes.setText("未卸载");
        });
        //创建弹窗对象
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        String str = String.format("选的的是%s年-%s月-%s日", year, month + 1, dayOfMonth);
        tv_showSelectDate.setText(str);
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String str = String.format("选的的是%s时-%s分", hourOfDay, minute);
        tv_showSelectTime.setText(str);
    }
}